package vip.wangwenhao.util;

import org.apache.commons.collections.CollectionUtils;
import org.codehaus.plexus.util.StringUtils;
import vip.wangwenhao.mojo.GeneralMojo;
import vip.wangwenhao.vo.ColumnInfo;

import java.io.File;
import java.util.List;

/**
 * @author wwh
 **/
public class GenEntityUtil {

    public static List<String> tablePrefixes;
    public static List<String> columnPrefixes;
    public static String beanPackage;
    public static String author;

    public static void init(GeneralMojo generalMojo) {
        tablePrefixes = generalMojo.getTablePrefixes();
        columnPrefixes = generalMojo.getColumnPrefixes();
        beanPackage = generalMojo.getBeanPackage();
        String author = generalMojo.getAuthor();
        if (StringUtils.isEmpty(author)) {
            author = "";
        }
        GenEntityUtil.author = author;
    }


    public static void createEntity(String tableName) {
        String content = parse(tableName);
        FileUtil.write(content, "java" + File.separator + beanPackage.replace(".", File.separator), StringUtil.initCap(tableName, true, tablePrefixes) + ".java");
    }


    /**
     * 功能：生成实体类主体代码
     *
     * @param tableName
     * @return
     */
    public static String parse(String tableName) {
        List<ColumnInfo> columnInfos = DatabaseUtil.getColumnInfos(tableName);
        StringBuffer sb = new StringBuffer();
        sb.append("package " + beanPackage + ";\r\n");
        sb.append("\r\n");

        sb.append("\r\n");
        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableName + " 实体类\r\n");
        sb.append(" * @author " + author + "\r\n");
        sb.append(" */ \r\n");
        //实体部分
        sb.append("public class " + StringUtil.initCap(tableName, true, tablePrefixes) + "{\r\n");
        processAllAttrs(sb, columnInfos);
        processAllMethod(sb, columnInfos);
        sb.append("}\r\n");
        return sb.toString();
    }

    /**
     * 功能：生成所有属性
     *
     * @param sb
     */
    private static void processAllAttrs(StringBuffer sb, List<ColumnInfo> columnInfos) {

        if (CollectionUtils.isNotEmpty(columnInfos)) {
            for (ColumnInfo columnInfo : columnInfos) {
                sb.append("\t/**\n" +
                        "\t * " + columnInfo.getColumnComment() + " \n" +
                        "\t */\n");
                sb.append("\tprivate " + columnInfo.getColumnType().getJavaType() + " " + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + ";\r\n\r\n");
            }
        }

    }

    /**
     * 功能：生成所有方法
     *
     * @param sb
     */
    private static void processAllMethod(StringBuffer sb, List<ColumnInfo> columnInfos) {
        if (CollectionUtils.isNotEmpty(columnInfos)) {
            for (ColumnInfo columnInfo : columnInfos) {
                sb.append("\tpublic void set" + StringUtil.initCap(columnInfo.getColumnName(), true, columnPrefixes) + "(" + columnInfo.getColumnType().getJavaType() + " " +
                        StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + "){\r\n");
                sb.append("\t\tthis." + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + " = " + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + ";\r\n");
                sb.append("\t}\r\n\r\n");
                sb.append("\tpublic " + columnInfo.getColumnType().getJavaType() + " get" + StringUtil.initCap(columnInfo.getColumnName(), true, columnPrefixes) + "(){\r\n");
                sb.append("\t\treturn this." + StringUtil.initCap(columnInfo.getColumnName(), false, columnPrefixes) + ";\r\n");
                sb.append("\t}\r\n\r\n");
            }
        }
    }
}